home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™ 1987-1994 / MacHack™ '91 / '91 Attendee Contributions / Rotate Src / rotate.c.old < prev    next >
Encoding:
Text File  |  1990-12-17  |  8.9 KB  |  400 lines  |  [TEXT/MPS ]

  1. /* Rotate.c is the Macintosh shell for the Rotate.a algorithms */
  2.  
  3. #include    <Quickdraw.h>
  4. #include    <Desk.h>
  5. #include    <Dialogs.h>
  6. #include    <Events.h>
  7. #include    <Fonts.h>
  8. #include    <Memory.h>
  9. #include    <Menus.h>
  10. #include    <OSEvents.h>
  11. #include    <Packages.h>
  12. #include    <Resources.h>
  13. #include    <SegLoad.h>
  14. #include    <Toolutils.h>
  15. #include    <Windows.h>
  16. #include     "Rotate.h"
  17.  
  18. typedef enum {Rot0, RotH, RotV, RotX,
  19.               RotL, RotR, RotFL, RotFR} RotationSw;
  20.  
  21. pascal void ROTATE(rotation, sourceMap, destMap)
  22. RotationSw rotation;
  23. BitMap *sourceMap, *destMap;
  24. extern;
  25.  
  26. /*    Global Data objects, used by routines external to main(). */
  27. Boolean         doneFlag;        /* Becomes TRUE when File/Quit chosen */
  28. WindowRecord    wRecord;
  29. WindowPtr        myWindow;        /* Referenced often */
  30. EventRecord     myEvent;
  31. WindowPtr        whichWindow;
  32. long            lastTick,tick;
  33. long            stretch ;         /* used to vary the bounds of the Pict */
  34.  
  35. /*******************************/
  36. /*    BitMap Image Declarations  */
  37. /*******************************/
  38.  
  39. #define    xInset        10
  40. #define    yInset        20
  41.  
  42. #define    MyLeft          0
  43. #define    MyRight        ((kWindowWidth - 3*xInset) / 2)
  44. #define    MyTop          0
  45. #define    MyBottom    MyRight
  46.  
  47. #define    leftInset    xInset
  48. #define    rightInset    (kWindowWidth - (MyRight - MyLeft) - xInset)
  49. BitMap sourceMap, destMap;
  50.  
  51. /***************************/
  52. /*    BitMap Image Routines  */
  53. /***************************/
  54.  
  55. InitImage(Image)
  56. BitMap *Image;
  57. {
  58.     int    myRowBytes  = ((MyRight - MyLeft + 15) / 16) * 2;
  59.     int    mySize        = myRowBytes * (MyBottom - MyTop);
  60.     
  61.     Image->baseAddr = NewPtr(mySize);    /* allocate memory */
  62.     Image->rowBytes = myRowBytes;
  63.     SetRect(&Image->bounds, MyLeft, MyTop, MyRight, MyBottom);
  64. }
  65.  
  66. BuildImage(Image)
  67. BitMap *Image;
  68. {                            /* Create an Image to rotate  */
  69.     GrafPort myPort,*savePort;
  70.  
  71.     GetPort(&savePort);
  72.     OpenPort(&myPort);
  73.     SetPortBits(Image);
  74.     RectRgn(myPort.visRgn, &Image->bounds);
  75.     PortSize(Image->bounds.right, Image->bounds.bottom);
  76.     SetPort(savePort);
  77.     ClosePort(&myPort);
  78. }
  79.  
  80. ShowImage(Image,myXoff,myYoff)
  81. BitMap *Image;
  82. short myXoff,myYoff;
  83. {
  84.     GrafPtr myPort;    
  85.  
  86.     GetPort(&myPort);
  87.     OffsetRect(&Image->bounds, myXoff,  myYoff);    
  88.     CopyBits(Image, &myPort->portBits, &Image->bounds, &Image->bounds, srcXor, 0L);     
  89.     OffsetRect(&Image->bounds, -myXoff, -myYoff);
  90. }
  91.  
  92. RotateImage(rotation, sourceMap, destMap)
  93. RotationSw rotation;
  94. BitMap *sourceMap, *destMap;
  95. {
  96.     if (destMap->baseAddr) {                    /* If there an old image */
  97.         ShowImage(destMap, rightInset, yInset);    /* Display image to erase it */
  98.         DisposPtr(destMap->baseAddr);            /*  then, free it */
  99.         destMap->baseAddr = 0;
  100.     }
  101.     WaitForTick();                                /* wait for the clock to tick */
  102.     ROTATE(rotation, sourceMap, destMap);        /* Do the rotation */
  103.     ShowTick(rotation);                            /* Time it */
  104.     ShowImage(destMap, rightInset, yInset);        /* Display the rotated image */
  105. }
  106.  
  107. GarbageImage(Image)
  108. BitMap *Image;
  109. {
  110.     int    size = Image->rowBytes * (Image->bounds.bottom - Image->bounds.top);
  111.     char *p = Image->baseAddr;
  112.     char *g = 100;            /* just some low memory address */
  113.     
  114.     ShowImage(Image, leftInset, yInset);    /* Display image to erase it */
  115.     while (size--)
  116.         *p++ = *g++;
  117.     ShowImage(Image, leftInset, yInset);    /* Display Image */
  118. }
  119.  
  120. FillImage(Image, color)
  121. BitMap *Image;
  122. int color;
  123. {
  124.     int    size = Image->rowBytes * (Image->bounds.bottom - Image->bounds.top);
  125.     char *p = Image->baseAddr;
  126.     
  127.     ShowImage(Image, leftInset, yInset);    /* Display image to erase it */
  128.     while (size--)
  129.         *p++ = color;
  130.     ShowImage(Image, leftInset, yInset);    /* Display Image */
  131. }
  132.  
  133. GetPict(Image)
  134. BitMap *Image;
  135. {
  136.     int refNum, picWidth, picHeight;
  137.     GrafPort myPort,*savePort;
  138.     PicHandle srcPict;
  139.     Rect *irp;
  140.  
  141.     if (Image->baseAddr) {                /* If there an old image */
  142.         ShowImage(Image, leftInset, yInset); /* Display image to erase it */
  143.         DisposPtr(Image->baseAddr);        /*    then, free it */
  144.         Image->baseAddr = 0;
  145.     }
  146. /* Load the PICT, compute target Rect to center in image window, draw it */
  147.     refNum = OpenResFile("\pBell");
  148.     srcPict = (PicHandle)GetResource('PICT', rBell); /* load PICT resource    */
  149.     if (srcPict == 0L) {
  150.         doAlert(rAlert,kNoBell);
  151.         ExitToShell();
  152.     }
  153.     HLock((Handle)srcPict);
  154.     irp = &((*srcPict)->picFrame);            /* pointer to PICT's bounds    */
  155.     picWidth = scale*(irp->right - irp->left);    /* compute PICT width        */
  156.     picHeight = scale*(irp->bottom - irp->top);    /* compute PICT height        */
  157.  
  158.     picWidth += stretch;
  159.     picHeight += stretch;
  160.  
  161.     Image->bounds.left = 0;                    /* PICT size Rect    */
  162.     Image->bounds.top = 0;
  163.     Image->bounds.right = Image->bounds.left + picWidth;
  164.     Image->bounds.bottom = Image->bounds.top + picHeight;
  165.     Image->rowBytes = ((picWidth + 15) / 16) * 2;
  166.     Image->baseAddr = NewPtr(Image->rowBytes * Image->bounds.bottom);
  167.  
  168.     GetPort(&savePort);
  169.     OpenPort(&myPort);
  170.     SetPortBits(Image);
  171.     RectRgn(myPort.visRgn, &Image->bounds);
  172.     PortSize(Image->bounds.right, Image->bounds.bottom);
  173.     DrawPicture(srcPict,&Image->bounds);
  174.     FrameRect(&Image->bounds);
  175.     SetPort(savePort);
  176.     ClosePort(&myPort);
  177.  
  178.     HUnlock((Handle)srcPict);             /* we no longer need resource */
  179.     CloseResFile(refNum);
  180.     ShowImage(Image, leftInset, yInset); /* Display sourceMap */
  181. }
  182.  
  183. /******************************/
  184. /*    Low level system routines */
  185. /******************************/
  186.  
  187. doAlert(theAlert,theInx)
  188. short theAlert,theInx;
  189. {
  190.     Str255 theText;
  191.  
  192.     GetIndString(theText,rTextStr,theInx);
  193.     SysBeep(4);
  194.     ParamText(theText,0,0,0);
  195.     Alert(theAlert,0);
  196. }
  197.  
  198. WaitForTick()
  199. {
  200.     tick = TickCount();
  201.     while (tick == TickCount())
  202.         ;    /* wait */
  203.     lastTick = tick;
  204. }
  205.  
  206. ShowNum(n,x,y)
  207. int n,x,y;
  208. {
  209.     Rect r;
  210.     Str255 myStr;
  211.  
  212.     NumToString(n, myStr);
  213.     SetRect(&r, x, y-10, x+40, y+2);
  214.     EraseRect(&r);
  215.     MoveTo(x,y);
  216.     DrawString(myStr);
  217. }
  218.  
  219. ShowTick(rotation)
  220. RotationSw rotation;
  221. {
  222.     int n,x,y;
  223.     
  224.     n = TickCount() - lastTick;
  225.     x = 160 + 40 * rotation;
  226.     y = 3*yInset + MyBottom;
  227.     MoveTo(90,y);
  228.     DrawString("\p Ticks = ");
  229.     ShowNum(n, x, y);
  230. }
  231.  
  232. ShowSize(sourceMap)
  233. BitMap *sourceMap;
  234. {
  235.     int x,y;
  236.     
  237.     x = 160;
  238.     y = 2*yInset + MyBottom;
  239.     MoveTo(90,y);
  240.     DrawString("\pBounds = ");
  241.     ShowNum(sourceMap->bounds.left, x, y);
  242.     x += 40;
  243.     ShowNum(sourceMap->bounds.top, x, y);
  244.     x += 40;
  245.     ShowNum(sourceMap->bounds.right, x, y);
  246.     x += 40;
  247.     ShowNum(sourceMap->bounds.bottom, x, y);
  248. }
  249.  
  250.  
  251. /****************************/
  252. /*    Event handling routines */
  253. /****************************/
  254.  
  255. doEvent()
  256. {
  257.     SystemTask();
  258.  
  259.     if (GetNextEvent(everyEvent, &myEvent))
  260.     switch (myEvent.what) {
  261.         case mouseDown:
  262.             switch (FindWindow(myEvent.where, &whichWindow)) {
  263.                 case inSysWindow:
  264.                     SystemClick(&myEvent, whichWindow);
  265.                     break;
  266.                 case inMenuBar:
  267.                     doCommand(MenuSelect(myEvent.where));
  268.                     break;
  269.                 case inContent:
  270.                     if (whichWindow != FrontWindow())
  271.                         SelectWindow(whichWindow);
  272.                     break;
  273.             }
  274.             break;
  275.     
  276.         case keyDown:
  277.         case autoKey:
  278.             if (myWindow == FrontWindow()) {
  279.                 if (myEvent.modifiers & cmdKey) {
  280.                     doCommand(MenuKey(myEvent.message & charCodeMask));
  281.                 }
  282.             }
  283.             break;
  284.     
  285.         case updateEvt:
  286.             if ((WindowPtr) myEvent.message == myWindow) {
  287.                 BeginUpdate(myWindow);
  288.                 EraseRect(&myWindow->portRect);
  289.                 ShowImage(&sourceMap, leftInset, yInset);/* Display image */
  290.                 ShowImage(&destMap, rightInset, yInset); /* Display destMap */
  291.                 ShowSize(&sourceMap);
  292.                 EndUpdate(myWindow);
  293.             }
  294.             break;
  295.     }
  296. }
  297.  
  298. setupMenus()
  299. {
  300.     Handle menuBar;
  301.  
  302.     menuBar = GetNewMBar(rMenuBar);            /* read menus into menu bar */
  303.     if (menuBar == nil) ExitToShell();
  304.     SetMenuBar(menuBar);                    /* install menus */
  305.     AddResMenu(GetMHandle(mApple), 'DRVR');    /* add DA names to Apple menu */
  306.     DrawMenuBar();
  307. }
  308.  
  309. NewScreen()
  310. {
  311.     myWindow = GetNewWindow(rWindow, nil, (WindowPtr) -1);
  312.     SetPort(myWindow);
  313. }
  314.  
  315. /* Process mouse clicks in menu bar */
  316. doCommand(mResult)
  317. long    mResult;
  318. {
  319.     short         theMenu, theItem;
  320.     char        daName[256];
  321.     GrafPtr     savePort;
  322.     RotationSw    rot;
  323.  
  324.     theMenu = HiWord(mResult);
  325.     theItem = LoWord(mResult);
  326.     switch (theMenu) {
  327.         case mApple:
  328.             if (theItem == aboutMeCommand)
  329.                 doAlert(rMsg,kAboutText);
  330.             else {
  331.                 GetItem(GetMHandle(mApple), theItem, daName);
  332.                 GetPort(&savePort);
  333.                 (void) OpenDeskAcc(daName);
  334.                 SetPort(savePort);
  335.             }
  336.             break;
  337.  
  338.         case mFile:
  339.             if (theItem == quitCommand)
  340.                 doneFlag = true;            /* Request exit */
  341.             break;
  342.  
  343.         case mImage:
  344.             switch (theItem) {
  345.             case GarbageCommand:
  346.                 GarbageImage(&sourceMap);
  347.                 break;
  348.             case BlackCommand:
  349.                 FillImage(&sourceMap, 0xFF);
  350.                 break;
  351.             case WhiteCommand:
  352.                 FillImage(&sourceMap, 0);
  353.                 break;
  354.             case BellCommand:
  355.                 stretch = 0;        /* reset stretch */
  356.                 GetPict(&sourceMap);
  357.                 break;
  358.             case StretchCommand:
  359.                 stretch++;
  360.                 GetPict(&sourceMap);
  361.                 break;
  362.             } /*endsw theItem*/
  363.             ShowSize(&sourceMap);    /* display the bounds */
  364.             break;
  365.  
  366.         case mRotate:
  367.             if (theItem == RotAllCommand) {
  368.                 for (rot=0; rot < 8; rot++)
  369.                     RotateImage(rot, &sourceMap, &destMap);
  370.             } else
  371.                 RotateImage(theItem - Rot0Command, &sourceMap, &destMap);
  372.             break;
  373.     }
  374.     HiliteMenu(0);
  375. }
  376.  
  377. main()
  378. {
  379.     MaxApplZone();
  380.     MoreMasters();
  381.  
  382. /*    Initialization traps    */
  383.     InitGraf(&qd.thePort);
  384.     InitFonts();
  385.     FlushEvents(everyEvent, 0);
  386.     InitWindows();
  387.     InitMenus();
  388.     TEInit();
  389.     InitDialogs(nil);
  390.     InitCursor();
  391.  
  392.     setupMenus();
  393.     doneFlag = false;
  394.     NewScreen();
  395.     InitImage(&sourceMap);
  396.     BuildImage(&sourceMap);
  397.     while (!doneFlag)
  398.         doEvent();
  399. }
  400.